package com.zlq.cn.controller.sign.lovers;


import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.dto.param.SendSmsParam;
import com.zlq.cn.dto.param.lovers.LoversLocationLogParam;
import com.zlq.cn.dto.param.lovers.LoversLoginParam;
import com.zlq.cn.dto.vo.SignCommonParam;
import com.zlq.cn.entity.LocationLog;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.un.LocationLogService;
import com.zlq.cn.service.lovers.LoversUserService;
import com.zlq.cn.service.un.SmsRecordService;
import com.zlq.cn.service.un.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 小情侣用户 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-03-19
 */
@Slf4j
@Api(tags = "小情侣-用户")
@RestController
@RequestMapping("/lovers/user")
public class LoversUserController {

    @Resource
    private UserService userService;

    @Resource
    private LoversUserService loversUserService;

    @Resource
    private SmsRecordService smsRecordService;

    @Resource
    private LocationLogService locationLogService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public SignApiResult<User> login(@Valid @RequestBody LoversLoginParam param) throws IOException {
        return SignApiResult.resultSuccess(loversUserService.login(param));
    }

    @ApiOperation("用户注销")
    @PostMapping("/cancellation")
    public SignApiResult<Boolean> cancellation(@RequestBody SignCommonParam commonParam) {
        return SignApiResult.resultSuccess(loversUserService.cancellation());
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/get/info")
    public SignApiResult<User> getUserInfo(@RequestBody SignCommonParam commonParam) {
        User loginUser = SignApiBootstrap.requestUser();
        loginUser.setCurrentDate(LocalDateTime.now());
        return SignApiResult.resultSuccess(loversUserService.toLoginUser(loginUser));
    }

    @ApiOperation("修改头像、昵称、生日、性别")
    @PostMapping("/update/info")
    public SignApiResult<String> updateUserInfo(@RequestBody User user) {
        userService.updateUserAvatarAndNameAndGender(user);
        return SignApiResult.resultSuccess("修改成功");
    }

    @ApiOperation("发送短信")
    @PostMapping("/send/sms")
    public SignApiResult<String> sendSms(@Valid @RequestBody SendSmsParam sendSmsParam) {
        smsRecordService.sendSmsCheckImei(sendSmsParam);
        return SignApiResult.resultSuccess("发送成功");
    }

    @ApiOperation("刷新用户在线状态")
    @PostMapping("/user/on/line")
    public SignApiResult<String> userOnline(@RequestBody SignCommonParam commonParam) {
        loversUserService.userOnline();
        return SignApiResult.resultSuccess("刷新用户在线状态成功");
    }

    @ApiOperation("批量上报位置信息")
    @PostMapping("/batch/report/location")
    public SignApiResult<String> batchReportLocation(@RequestBody LocationLog locationLog) {
        User loginUser = SignApiBootstrap.requestUser();
        locationLog.setUserId(loginUser.getId());
        locationLog.setCreateTime(LocalDateTime.now());
        locationLog.setUpdateTime(LocalDateTime.now());
        LocationLogService.addLocationLog(locationLog);
        return SignApiResult.resultSuccess("上报成功");
    }

    @ApiOperation("获取位置信息")
    @PostMapping("/location/log/list")
    public SignApiResult<List<LocationLog>> locationLogList(@Valid @RequestBody LoversLocationLogParam param) {
        return SignApiResult.resultSuccess(
                locationLogService.lambdaQuery()
                        .eq(LocationLog::getUserId, param.getUserId())
                        .between(LocationLog::getCreateTime, param.getStartTime(), param.getEndTime())
                        .list()
        );
    }

    @ApiOperation("删除位置信息")
    @PostMapping("/delete/location/log")
    public SignApiResult<String> clearLocationLog(@RequestBody SignCommonParam commonParam) {
        User loginUser = SignApiBootstrap.requestUser();
        if (userService.updateLocationResetCount(loginUser)) {
            locationLogService.lambdaUpdate()
                    .eq(LocationLog::getUserId, loginUser.getId())
                    .remove();
        }
        return SignApiResult.resultSuccess("清除成功");
    }
}

